package com.magnificent.atlantis.client.data.configuration;

import com.magnificent.atlantis.client.data.task.planner.AbstractPlannerTask;
import com.magnificent.commons.utils.DateUtils;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.*;

/**
 * User: Babanin
 * Date: 13.02.2009
 * Time: 16:01:03
 */
@XmlRootElement(name = "configurations")
public class ConfigurationContainer {
    private List<AbstractPlannerTask> dataPlannerTasks;
    private List<AbstractConfiguration> configurations;

    public ConfigurationContainer() {
        dataPlannerTasks = new ArrayList<AbstractPlannerTask>();
        configurations = new ArrayList<AbstractConfiguration>();
    }

    @XmlElement()
    public List<AbstractPlannerTask> getDataTasks() {
        return dataPlannerTasks;
    }

    public void setDataTasks(List<AbstractPlannerTask> dataPlannerTasks) {
        this.dataPlannerTasks = dataPlannerTasks;
    }

    @XmlElement()
    public List<AbstractConfiguration> getConfigurations() {
        return configurations;
    }

    public List<ServerConfiguration> getServerConfigurations() {
        List<ServerConfiguration> result = new ArrayList<ServerConfiguration>();

        for (AbstractConfiguration configuration : configurations) {
            if (configuration instanceof ServerConfiguration) {
                result.add((ServerConfiguration) configuration);
            }
        }

        return result;
    }

    public List<LocalConfiguration> getLocalConfigurations() {
        List<LocalConfiguration> result = new ArrayList<LocalConfiguration>();

        for (AbstractConfiguration configuration : configurations) {
            if (configuration instanceof LocalConfiguration) {
                result.add((LocalConfiguration) configuration);
            }
        }

        return result;
    }

    public AbstractConfiguration getConfigurationById(int id) {
        for (AbstractConfiguration configuration : configurations) {
            if (configuration.getId() == id) {
                return configuration;
            }
        }

        return null;
    }

    public List<AbstractPlannerTask> getIntersectedTasks(AbstractPlannerTask task) {
        List<AbstractPlannerTask> result = new ArrayList<AbstractPlannerTask>();
        Date end = new Date(task.getStartDate().getTime() + task.getDuration());

        for (AbstractPlannerTask plannerTask : dataPlannerTasks) {
            if (!plannerTask.isScheduled()) {
                continue;
            }

            if (DateUtils.between(task.getStartDate(), plannerTask.getStartDate(), end) ||
                    DateUtils.between(task.getStartDate(), plannerTask.getEndDate(), end)) {
                result.add(plannerTask);
            }
        }

        return result;
    }

    public void setDataConfigurations(List<AbstractConfiguration> localConfigurations) {
        this.configurations = localConfigurations;

        Collections.sort(localConfigurations, new Comparator<AbstractConfiguration>() {
            public int compare(AbstractConfiguration o1, AbstractConfiguration o2) {
                return o1.getId() - o2.getId();
            }
        });
    }
}
